package chapter18;

import java.io.*;
import java.time.Instant;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author liuziyang
 * @create 2019-03-30 22:14
 */
public class Logon implements Serializable {
    private Date date = new Date();
    private String username;
    private transient String password;

    public Logon(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Logon a = new Logon("hulk", "mypass");
        System.out.println("a=" + a);
        ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("logon.out"));
        o.writeObject(a);
        o.close();

        TimeUnit.SECONDS.sleep(1);
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("logon.out"));
        System.out.println("recovering object at " + Instant.now());
        a = (Logon) in.readObject();
        System.out.println("a=" + a);
    }

    @Override
    public String toString() {
        return "Logon{" +
                "date=" + date +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}
